<?php

Runtime::startTimer();

class Runtime {

	// Speichert die Instanz der Klasse
    private static $instance;
    
    private static $startTime;
    
    private static $pageRequest; 
    
    /**
     * Stores the current active Page
     *
     * @var Page
     */
    private static $page;
	
    /**
     * Stores the page wich is currently processes (in a nestedPage envoirement)
     *
     * @var Page
     */
    private static $mainPage;
    
    private static $configuration;
    
    // Ein private Konstruktor; verhindert die direkte Erzeugung des Objektes
    private function __construct() {
    	
	}

    /**
     * Returns the Singleton-Instance of the Class
     *
     */
    public static function getInstance() {
    	
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }
	
	private static function micro_time()
		{
			$timearray = explode(" ", microtime());
			return ($timearray[1] + $timearray[0]);
		}
    	
    public static function startTimer() {
    	self::$startTime = self::micro_time(true);
    }
    
    public static function getTimer() {
    	return self::micro_time() - self::$startTime;
    }
	
    public static function setCurrentMainPage($p) {
    	self::$mainPage = $p;
    }
    
    public static function setCurrentPage($p) {
    	self::$page = $p;
    }
    
    public static function setCurrentRequest($r) {
    	self::$pageRequest = $r;
    }
    /**
     * 
     * @return SecurityUser
     */
    public static function getCurrentUser() {
    	return SecurityUser::getUser();
    }
    
    public static function getSystemVersion() {
    	
    	$cache_id = "SystemVersion";
    	if(!$_SESSION["cache_".$cache_id]) {
    		$ts = Utils::getNewestDate(eCMS_DIR . "/app", "class.php");
    		$date =  floatval(date("y", $ts)) . "." . floatval(date("m", $ts)) . "." . floatval(date("d", $ts)) ." (Build " . date("Hi", $ts) .")";
    		
    		$_SESSION["cache_".$cache_id] = $date;

    	}
   		return $_SESSION["cache_".$cache_id];
     }
    
    public static function getModuleVersion() {
     	
    	$cache_id = "Module" . self::$mainPage->getModuleName() . "Version";
    	if(!$_SESSION["cache_".$cache_id]) {
    		
    		if(is_dir(eCMS_DIR . "/module/core/" . self::$mainPage->getModuleName())) {
    			$ts = Utils::getNewestDate(eCMS_DIR . "/module/core/" . self::$mainPage->getModuleName(), ".php");
    		}
    		else {
    			$ts = Utils::getNewestDate(eCMS_DIR . "/module/user/" . self::$mainPage->getModuleName(), ".php");
    			
    		}
    		$date =  floatval(date("y", $ts)) . "." . floatval(date("m", $ts)) . "." . floatval(date("d", $ts)) ." (Build " . date("Hi", $ts) .")";
    		
    		$_SESSION["cache_".$cache_id] = $date;

    	}

   		return $_SESSION["cache_".$cache_id];
       	
    }
    
    
	/**
     * Returns the Page wich is the mainly requested Page (no nestedpages here)
     *
     * @return Page
     */
    public static function getCurrentMainPage() {
    	return self::$mainPage;
    }
    
    
    
	/**
     * Returns the Page wich is currently running (can be a nested Page, too)
     *
     * @return Page
     */
    public static function getCurrentPage() {
    	return self::$page;
    }
    
    
    public static function setConfiguration($c) {
    	self::$configuration = $c;
    }
    
    public static function getConfiguration($name=null) {
    	
    	//DEL:Trick 77 to prevent warning of Zend Studio
    	//$cfg = array();
    	//include 'include/default.conf.php';
    	if(!$name) {
    		return self::$configuration;
    	}
    	else {
    		return self::$configuration[$name];
    	}
    	
    }
    
   public static function getSetting($name, $fkModule=null, $fkModuleInstance=null, $default=null) {
    	
    	$cfg = self::getConfiguration();
    	$conn = MySQLConnection::getInstance();
    	
		$sql = "SELECT value, type FROM " . $cfg['table_prefix'] . "Setting 
				WHERE name = %n AND ( fkModule IS NULL OR (fkModule = %i AND fkModuleInstance IS NULL) OR (fkModule = %i AND fkModuleInstance = %i) )
				ORDER BY fkModuleInstance, fkModule DESC LIMIT 1"; 
		
		
		list($value, $type) = $conn->quickQuery($sql, array($name, $fkModule, $fkModule, $fkModuleInstance));
		
		if($type=="array") {

			$value = split("\n", str_replace("\l", "", str_replace("\r", "", $value)));
			
			while(list($id, $text)=each($value)) {
				$temp = split("=",$text,2);
				$arr[$temp[0]] = $temp[1];
			}
			$value = $arr;
		}
		
		if(!$value && $default) { $value = $default; }
		
		return $value;		
    }
    
    public static function getDataStore($fkModuleInstance, $subfolder=null) {

		$cfg = Runtime::getConfiguration();
		$conn = MySQLConnection::getInstance();
		
		// Get Module Name
		$modName = $conn->quickQuery("SELECT m.name FROM tblModuleInstance AS i LEFT JOIN tblModule AS m ON m.pkModule = i.fkModule WHERE pkModuleInstance = %i", array($fkModuleInstance));
		
		
		//Create Base-Dir for Module-Data
		$dir = $cfg['paths']['page'] . "/content/module";
		$folder = $modName . "/" . $subfolder;
		
		$folder = str_replace("\\", "/", $folder);
		$folders = split("/", $folder);

		
		for($i=0;$i<sizeof($folders);$i++) {
			
			//Create Subfolder if not exists
			$dir.= "/" . $folders[$i];
			if(!file_exists($dir)) {
				$old_umask = umask(0);
				mkdir($dir,0777); // the default chmod
				umask($old_umask);
			}
			
		}
		return $dir;     	
    	
    }
    
    /**
     * Adds the Message to the Log, and returns a Error-Page, if found.
     * 
     *  @return Page
     */
    
    public static function logEvent($friendly, $file, $classname, $function, $line, $vardata=null, Page $page=null) {

    	logger::logEvent($friendly, $file, $classname, $function, $line, $vardata, $page);
    	
    }
    // Halte Benutzer vom Klonen der Instanz ab
    public function __clone() {
    	
        trigger_error('Klonen ist nicht erlaubt.', E_USER_ERROR);
    }
}

?>